import { DataSet } from 'choerodon-ui/pro';
import { isBoolean } from 'lodash';

export default ({ newCodeFailedConditionsDs, fullAmountCodeFailedConditionsDs, stepDataSet }:{
  newCodeFailedConditionsDs: DataSet,
  fullAmountCodeFailedConditionsDs: DataSet
  stepDataSet: DataSet
}) => ({
  autoQuery: false,
  autoCreate: true,
  children: {
    newCodeFailedConditions: newCodeFailedConditionsDs,
    fullAmountCodeFailedConditions: fullAmountCodeFailedConditionsDs,
  },
  fields: [
    {
      name: 'gatesEnable',
      type: 'boolean' as any,
      defaultValue: false,
    },
    {
      name: 'gatesBlockAfterFail',
      type: 'boolean' as any,
      defaultValue: true,
    },
  ],
  events: {
    update: ({
      name, value, record, dataSet,
    }: any) => {
      if (name === 'gatesEnable') {
        if (!newCodeFailedConditionsDs.records.length
          && !fullAmountCodeFailedConditionsDs.records.length) {
          newCodeFailedConditionsDs.create(); // 保证进来的校验通过
        }
        if (!value) {
          stepDataSet.setState('qualityAccessControlValidate', true);
        }
        if (value && !isBoolean(record.get('gatesBlockAfterFail'))) {
          record.set('gatesBlockAfterFail', true);
        }
      }
    },
  },
});
